package com.listeningframework.boot.commons.utils;

import com.listeningframework.boot.commons.string.StringUtils;

import java.util.*;

/**
 * @author lixining
 * @version $Id: CommonCollectionUtils.java, v 0.1 2015年9月10日 上午11:27:53 lixining Exp $
 *          Common Collection Utils
 */
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {


    /**
     * String Convert To List According delimiters
     *
     * @param str               内容
     * @param trimTokens        结果是否去空格
     * @param delimiters        分隔符
     * @param ignoreEmptyTokens 忽略空结果
     * @return String[]
     */
    public static String[] tokenizeToArray(String str, String delimiters, boolean trimTokens,
                                           boolean ignoreEmptyTokens) {
        if (str == null) {
            return null;
        }
        StringTokenizer st = new StringTokenizer(str, delimiters);
        List<String> tokens = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (trimTokens) {
                token = token.trim();
            }
            if (!ignoreEmptyTokens || token.length() > 0) {
                tokens.add(token);
            }
        }
        return tokens.toArray(new String[tokens.size()]);
    }

    /**
     * String Convert To List According delimiters
     *
     * @param content    内容
     * @param delimiters 分隔符
     * @return List
     */
    public static List<String> tokenizeToList(String content, String delimiters) {
        List<String> result = new ArrayList<String>();
        if (StringUtils.isBlank(content)) {
            return result;
        }
        if (StringUtils.isBlank(delimiters)) {
            result.add(content);
            return result;
        }
        StringTokenizer st = new StringTokenizer(content, delimiters);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (StringUtils.isBlank(token)) {
                continue;
            }
            result.add(token.trim());
        }
        return result;
    }

    /**
     * String Convert To List According delimiters
     *
     * @param content    内容
     * @param delimiters 分隔符
     * @return List
     */
    public static List<String> tokenizeToUniqueList(String content, String delimiters) {
        List<String> result = new ArrayList<String>();
        if (StringUtils.isBlank(content)) {
            return result;
        }
        if (StringUtils.isBlank(delimiters)) {
            result.add(content);
            return result;
        }
        StringTokenizer st = new StringTokenizer(content, delimiters);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (StringUtils.isBlank(token) || result.contains(token)) {
                continue;
            }
            result.add(token.trim());
        }
        return result;
    }

    /**
     * String Convert To List According delimiters
     *
     * @param content 内容
     * @return List
     */
    public static List<String> tokenizeToUniqueList(String content) {
        return tokenizeToUniqueList(content, ",");
    }

    /**
     * String Convert To List According delimiters
     *
     * @param content 内容
     * @return List
     */
    public static List<String> tokenizeToList(String content) {
        return tokenizeToList(content, ",");
    }

    /**
     * String convert to {@link String}[] by delimiters
     *
     * @param content    内容
     * @param delimiters 分割符
     * @return String[]
     */
    public static String[] tokenizeToArray(String content, String delimiters) {
        List<String> resultList = tokenizeToList(content, delimiters);
        return resultList.toArray(new String[resultList.size()]);
    }

    /**
     * String convert to String[] by delimiters
     *
     * @param content 内容
     * @return String[]
     */
    public static String[] tokenizeToArray(String content) {
        return tokenizeToArray(content, ",");
    }

    /**
     * String convert to String[] by delimiters
     *
     * @param content    内容
     * @param delimiters 分隔符
     * @return String[]
     */
    public static String[] tokenizeToUniqueArray(String content, String delimiters) {
        Set<String> resultSet = tokenizeToSet(content, delimiters);
        return resultSet.toArray(new String[resultSet.size()]);
    }

    /**
     * String convert to String by delimiters
     *
     * @param content 内容
     * @return String[]
     */
    public static String[] tokenizeToUniqueArray(String content) {
        return tokenizeToArray(content, ",");
    }

    /**
     * String convert to Set by delimiters
     *
     * @param content    内容
     * @param delimiters 分割符
     * @return Set
     */
    public static Set<String> tokenizeToSet(String content, String delimiters) {
        Set<String> result = new HashSet<String>();
        result.addAll(tokenizeToList(content, delimiters));
        return result;
    }

    /**
     * String convert to Set by delimiters
     *
     * @param content 内容
     * @return Set
     */
    public static Set<String> tokenizeToSet(String content) {
        return tokenizeToSet(content, ",");
    }

    /**
     * Convert List to uppercase List
     *
     * @param list 源类表
     * @return List
     */
    public static final List<String> upperList(List<String> list) {
        List<String> result = new ArrayList<String>();
        if (org.apache.commons.collections.CollectionUtils.isEmpty(list)) {
            return result;
        }
        for (String val : list) {
            if (StringUtils.isBlank(val)) {
                continue;
            }
            result.add(val.toUpperCase());
        }
        return result;
    }

}
